<?php
$install_success = false;
$error_message = '';
$use_existing_db = isset($_POST['use_existing_db']) && $_POST['use_existing_db'] == '1';

// 处理表单提交
if ($_POST) {
    try {
        $db_config = [
            'host' => $_POST['db_host'],
            'user' => $_POST['db_user'],
            'pass' => $_POST['db_pass'],
            'name' => $_POST['db_name']
        ];
        
        // 测试数据库连接
        $dsn = "mysql:host={$db_config['host']};charset=utf8mb4";
        $pdo = new PDO($dsn, $db_config['user'], $db_config['pass']);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        // 如果使用现有数据库，则跳过创建数据库步骤
        if ($use_existing_db) {
            $pdo->exec("USE `{$db_config['name']}`");
        } else {
            // 创建数据库
            $pdo->exec("CREATE DATABASE IF NOT EXISTS `{$db_config['name']}`");
            $pdo->exec("USE `{$db_config['name']}`");
        }
        
        // 创建数据表
        $pdo->exec("CREATE TABLE IF NOT EXISTS `secert` (
            `id` INT AUTO_INCREMENT PRIMARY KEY,
            `file_md5` CHAR(32) NOT NULL UNIQUE COMMENT '文件MD5哈希值',
            `password` TEXT NOT NULL COMMENT '解压密码',
            `file_size` BIGINT UNSIGNED DEFAULT NULL COMMENT '文件大小(字节)',
            `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `file_name` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
            INDEX `idx_file_md5` (`file_md5`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
        
        // 生成 config.php 文件
        $config_content = "<?php\nreturn " . var_export($db_config, true) . ";\n";
        file_put_contents('config.php', $config_content);
        
        $install_success = true;
    } catch (Exception $e) {
        $error_message = '安装失败: ' . $e->getMessage();
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统初始化 - 压缩文件云解密平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .container {
            width: 100%;
            max-width: 700px;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        
        header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            text-align: center;
            padding: 30px 20px;
        }
        
        header h1 {
            font-size: 1.8rem;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }
        
        header p {
            font-size: 1rem;
            opacity: 0.9;
        }
        
        .content {
            padding: 30px;
        }
        
        .requirements {
            background: #e3f2fd;
            border-left: 4px solid #2196f3;
            padding: 15px;
            margin-bottom: 25px;
            border-radius: 0 8px 8px 0;
        }
        
        .requirements h3 {
            color: #1976d2;
            margin-bottom: 8px;
        }
        
        .requirements ul {
            padding-left: 20px;
        }
        
        .requirements li {
            margin-bottom: 5px;
            color: #555;
        }
        
        .section-title {
            color: #333;
            margin-bottom: 20px;
            font-size: 1.4rem;
            display: flex;
            align-items: center;
        }
        
        .section-title::before {
            content: "";
            display: inline-block;
            width: 6px;
            height: 24px;
            background: linear-gradient(to bottom, #4facfe, #00f2fe);
            margin-right: 12px;
            border-radius: 3px;
        }
        
        .scenario-selector {
            display: flex;
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .scenario-card {
            flex: 1;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            padding: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .scenario-card:hover {
            border-color: #4facfe;
            box-shadow: 0 5px 15px rgba(79, 172, 254, 0.1);
        }
        
        .scenario-card.selected {
            border-color: #4facfe;
            background: rgba(79, 172, 254, 0.05);
            box-shadow: 0 5px 15px rgba(79, 172, 254, 0.1);
        }
        
        .scenario-card h3 {
            color: #333;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
        }
        
        .scenario-card h3::before {
            content: "✓";
            display: inline-block;
            width: 20px;
            height: 20px;
            background: #e0e0e0;
            border-radius: 50%;
            text-align: center;
            line-height: 20px;
            margin-right: 10px;
            font-size: 12px;
        }
        
        .scenario-card.selected h3::before {
            background: #4caf50;
            color: white;
        }
        
        .scenario-card p {
            color: #666;
            font-size: 0.9rem;
            line-height: 1.5;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #555;
        }
        
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 14px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
            outline: none;
        }
        
        input[type="text"]:focus,
        input[type="password"]:focus {
            border-color: #4facfe;
            box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
        }
        
        .hidden {
            display: none;
        }
        
        .btn {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            border: none;
            padding: 14px 28px;
            font-size: 16px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 600;
            display: inline-block;
            text-align: center;
            text-decoration: none;
            width: 100%;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(79, 172, 254, 0.3);
        }
        
        .btn:active {
            transform: translateY(0);
        }
        
        .success-message {
            background: #e8f5e9;
            border-left: 4px solid #4caf50;
            padding: 20px;
            margin-bottom: 25px;
            border-radius: 0 8px 8px 0;
            text-align: center;
        }
        
        .success-message h2 {
            color: #388e3c;
            margin-bottom: 10px;
        }
        
        .error-message {
            background: #ffebee;
            border-left: 4px solid #f44336;
            padding: 15px;
            margin-bottom: 25px;
            border-radius: 0 8px 8px 0;
        }
        
        .error-message p {
            color: #c62828;
        }
        
        .enter-link {
            display: inline-block;
            background: #4caf50;
            color: white;
            padding: 12px 24px;
            text-decoration: none;
            border-radius: 6px;
            font-weight: 500;
            transition: background 0.3s;
        }
        
        .enter-link:hover {
            background: #388e3c;
        }
        
        footer {
            text-align: center;
            padding: 20px;
            color: #666;
            font-size: 0.9rem;
            background: #f8f9fa;
        }
        
        @media (max-width: 768px) {
            body {
                padding: 10px;
            }
            
            .container {
                border-radius: 10px;
            }
            
            header h1 {
                font-size: 1.5rem;
            }
            
            .scenario-selector {
                flex-direction: column;
                gap: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>压缩文件云解密平台</h1>
            <p>系统初始化配置</p>
        </header>
        
        <div class="content">
            <?php if ($install_success): ?>
                <div class="success-message">
                    <h2>✅ 安装成功!</h2>
                    <p>数据库和配置文件已创建完成。</p>
                    <a href="index.html" class="enter-link">进入程序</a>
                </div>
            <?php else: ?>
                <?php if ($error_message): ?>
                    <div class="error-message">
                        <p>❌ <?php echo htmlspecialchars($error_message); ?></p>
                    </div>
                <?php endif; ?>
                
                <div class="requirements">
                    <h3>系统要求</h3>
                    <ul>
                        <li>PHP 5.6 或更高版本</li>
                        <li>PDO MySQL 扩展</li>
                        <li>MySQL 5.6 或更高版本</li>
                    </ul>
                </div>
                
                <h2 class="section-title">部署场景选择</h2>
                <div class="scenario-selector">
                    <div class="scenario-card <?php echo !$use_existing_db ? 'selected' : ''; ?>" onclick="selectScenario('new')">
                        <h3>全新安装</h3>
                        <p>适用于有权限创建新数据库的情况。系统将自动创建数据库和数据表。</p>
                    </div>
                    <div class="scenario-card <?php echo $use_existing_db ? 'selected' : ''; ?>" onclick="selectScenario('existing')">
                        <h3>使用现有数据库</h3>
                        <p>适用于虚拟主机环境，需使用控制面板预先创建的数据库。</p>
                    </div>
                </div>
                
                <h2 class="section-title">数据库配置</h2>
                <form method="post" id="installForm">
                    <input type="hidden" name="use_existing_db" id="use_existing_db" value="<?php echo $use_existing_db ? '1' : '0'; ?>">
                    
                    <div class="form-group">
                        <label for="db_host">数据库服务器</label>
                        <input type="text" id="db_host" name="db_host" placeholder="请输入数据库服务器地址" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="db_user">数据库用户名</label>
                        <input type="text" id="db_user" name="db_user" placeholder="请输入数据库用户名" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="db_pass">数据库密码</label>
                        <input type="password" id="db_pass" name="db_pass" placeholder="请输入数据库密码">
                    </div>
                    
                    <div class="form-group">
                        <label for="db_name">数据库名称</label>
                        <input type="text" id="db_name" name="db_name" placeholder="请输入数据库名称" required>
                    </div>
                    
                    <button type="submit" class="btn">开始安装</button>
                </form>
            <?php endif; ?>
        </div>
        
        <footer>
            <p>© 2025 小沙盒工作室 - 压缩文件云解密平台</p>
        </footer>
    </div>
    
    <script>
        function selectScenario(type) {
            const newCard = document.querySelector('.scenario-card:nth-child(1)');
            const existingCard = document.querySelector('.scenario-card:nth-child(2)');
            const hiddenInput = document.getElementById('use_existing_db');
            
            if (type === 'new') {
                newCard.classList.add('selected');
                existingCard.classList.remove('selected');
                hiddenInput.value = '0';
            } else {
                existingCard.classList.add('selected');
                newCard.classList.remove('selected');
                hiddenInput.value = '1';
            }
        }
    </script>
</body>
</html>